package com.nwpu.intimacy.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nwpu.intimacy.R;
import com.nwpu.intimacy.utils.CacheUtil;
import com.nwpu.intimacy.utils.LanguageComparator_CN;
import com.nwpu.intimacy.utils.PinyinUtils;

public class PinyinAdapter extends BaseExpandableListAdapter {

	// 字符串
	private List<String> strList;

	private PinyinUtils assort = new PinyinUtils();

	private Context context;

	private LayoutInflater inflater;
	// 中文排序
	private LanguageComparator_CN cnSort = new LanguageComparator_CN();

	public PinyinAdapter(Context context, List<String> strList) {
		super();
		this.context = context;
		this.inflater = LayoutInflater.from(context);
		this.strList = strList;
		if (strList == null) {
			strList = new ArrayList<String>();
		}

		// 排序
		sort();

	}

	private void sort() {
		// 分类
		for (String str : strList) {
			assort.getHashList().add(str);
		}
		assort.getHashList().sortKeyComparator(cnSort);
		for (int i = 0, length = assort.getHashList().size(); i < length; i++) {
			Collections.sort((assort.getHashList().getValueListIndex(i)),
					cnSort);
		}

	}

	public void clear() {

		strList.clear();
	}

	public Object getChild(int group, int child) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueIndex(group, child);
	}

	public long getChildId(int group, int child) {
		// TODO Auto-generated method stub
		return child;
	}

	public View getChildView(int group, int child, boolean arg2,
			View contentView, ViewGroup arg4) {
		// TODO Auto-generated method stub
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.contactslist_adapter_chat,
					null);
		}
		TextView textView = (TextView) contentView
				.findViewById(R.id.user_friend_name);
		textView.setText(assort.getHashList().getValueIndex(group, child));

		ImageView head_user = (ImageView) contentView
				.findViewById(R.id.head_user);

		Bitmap photo = CacheUtil.GetFromFile(assort.getHashList()
				.getValueIndex(group, child), "FriendPhoto");
		Drawable drawable = new BitmapDrawable(photo);

		head_user.setImageDrawable(drawable);

		return contentView;
	}

	public int getChildrenCount(int group) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueListIndex(group).size();
	}

	public Object getGroup(int group) {
		// TODO Auto-generated method stub
		return assort.getHashList().getValueListIndex(group);
	}

	public int getGroupCount() {
		// TODO Auto-generated method stub
		return assort.getHashList().size();
	}

	public long getGroupId(int group) {
		// TODO Auto-generated method stub
		return group;
	}

	public View getGroupView(int group, boolean arg1, View contentView,
			ViewGroup arg3) {
		if (contentView == null) {
			contentView = inflater.inflate(R.layout.contactslist_group_item,
					null);
			contentView.setClickable(true);
		}
		TextView textView = (TextView) contentView
				.findViewById(R.id.name_group_friend);

		if (!assort.getFirstChar(assort.getHashList().getValueIndex(group, 0))
				.equals("")) {
			textView.setText(assort.getFirstChar(assort.getHashList()
					.getValueIndex(group, 0)));

		}

		// 禁止伸展

		return contentView;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	public PinyinUtils getAssort() {
		return assort;
	}

}
